function convert_fgdir( fgdir, outdir )
% This script assumes that the file structure is, for example:
% The path to the FG-segmentation of an image is at:
% WHATEVER\BSDS300\fgdata\color\1103\181091-104.fg
% Which corresponds to this segmentation file at this path
% WHATEVER\BSDS300\human\color\1103\181091.fg
%
% In other words, that "fgdata" can be replaced with "human" to get the 
% corresponding segmentation directory.

infiles = dir( fgdir );

for i = 3:length(infiles)
    if( infiles(i).isdir )
        convert_fgdir( fullfile( fgdir, infiles(i).name ), outdir );
    else
        fgfn = fullfile( fgdir, infiles(i).name );
        pos = strfind(fgfn, 'fgdata');
        segfn = [fgfn(1:pos(end)-1) 'human' fgfn(pos(end)+6:end) ];
        segfn = regexprep(segfn, '(\d+)-\d+\.\w+$', '$1.seg');
        
        outfn = fullfile( outdir, regexprep(fgfn, '^.*[/\\](\d+)[/\\](\d+)-(\d+)\.\w+$', '$2-seg$1-fg$3.png'));
        outfnseg = fullfile( outdir, regexprep(fgfn, '^.*[/\\](\d+)[/\\](\d+)-(\d+)\.\w+$', '$2-seg$1.png'));
        convert_fg( segfn, fgfn, outfn, outfnseg );
    end
end